package com.situ.day17;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
	
	public static void main(String[] args) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Scanner scanner = new Scanner(System.in);
		
		while (true) {
			System.out.println("---------------");
			System.out.println("查询请输入1");
			System.out.println("更新请输入2");
			System.out.println("删除请输入3");
			System.out.println("插入请输入4");
			System.out.println("退出请输入0");
			System.out.println("---------------");
			int count = scanner.nextInt();
			if (count == 0) {
				System.out.println("退出程序");
				break;
			}
			switch (count) {
			//查询
			case 1:
				try {
					connection = JDBCUtil.getConnection();
					String sql = "select id,`name`,age,gender from student where id = ?";
					preparedStatement = connection.prepareStatement(sql);
					System.out.println("请输入要查询的id：");
					int stuId = scanner.nextInt();
					preparedStatement.setInt(1, stuId);
					resultSet =  preparedStatement.executeQuery();
					ArrayList<Student> list = new ArrayList<Student>();
					while (resultSet.next()) {
						int id = resultSet.getInt("id");
						String name = resultSet.getString("name");
						int age = resultSet.getInt("age");
						String gender = resultSet.getString("gender");
						Student student = new Student(id, name, age, gender);
						list.add(student);
					}
					for (Student student : list) {
						System.out.println(student);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}finally {
					JDBCUtil.close(connection, preparedStatement, resultSet);
				}
				break;
			//更新
			case 2:
				try {
					connection = JDBCUtil.getConnection();
					String sql = "update student set `name` = ?,age = ?,gender = ? where id =?";
					preparedStatement = connection.prepareStatement(sql);
					System.out.println("请输入输入要修改学生的id：");
					int stuId = scanner.nextInt();
					preparedStatement.setInt(4, stuId);
					System.out.println("将name修改为 ：");
					String stuName = scanner.next();
					preparedStatement.setString(1, stuName);
					System.out.println("将age修改为：");
					int stuAge = scanner.nextInt();
					preparedStatement.setInt(2,stuAge);
					System.out.println("将gender修改为：");
					String stuGender = scanner.next();
					preparedStatement.setString(3, stuGender);
					int count1 = preparedStatement.executeUpdate();
					System.out.println(count1);
					if (count1 == 1) {
						System.out.println("更新成功");
					}else {
						System.out.println("更新失败");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}finally {
					JDBCUtil.close(connection, preparedStatement, null);
				}
				break;
				//删除
			case 3:
				try {
					connection = JDBCUtil.getConnection();
					String sql = "delete from student where id = ?";
					preparedStatement = connection.prepareStatement(sql);
					System.out.println("删除id为多少的学生信息：");
					int stuId = scanner.nextInt();
					preparedStatement.setInt(1, stuId);
					int count2 = preparedStatement.executeUpdate();
					System.out.println(count2);
					if (count2 == 1) {
						System.out.println("删除成功");
					}else {
						System.out.println("删除失败");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}finally {
					JDBCUtil.close(connection, preparedStatement, null);
				}
				break;
				
				//插入
			case 4:
				try {
					connection = JDBCUtil.getConnection();
					String sql = "INSERT INTO student(id,`name`,age,gender) VALUES(?,?,?,?)";
					preparedStatement = connection.prepareStatement(sql);
					System.out.println("插入的id为：");
					int stuId = scanner.nextInt();
					preparedStatement.setInt(1, stuId);
					System.out.println("插入的名字为：");
					String stuName = scanner.next();
					preparedStatement.setString(2, stuName);
					System.out.println("插入的年龄为：");
					int stuAge = scanner.nextInt();
					preparedStatement.setInt(3, stuAge);
					System.out.println("插入的性别为：");
					String stuGender = scanner.next();
					preparedStatement.setString(4, stuGender);
					int count3 = preparedStatement.executeUpdate();
					System.out.println(count3);
					if (count3 == 1) {
						System.out.println("插入成功");
					}else {
						System.out.println("插入失败");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}finally {
					JDBCUtil.close(connection, preparedStatement, resultSet);
				}
				break;
				
			default:
				System.out.println("输入不合法");
				break;
			}
		}
	}
}
